home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / sveditmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  14.8 KB  |  615 lines

  1. /*
  2.     File:        SVEditMain.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                             3.1 updates by Greg Sutton.    
  8.  
  9.     Copyright:    Copyright ©1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 11/1/95        DS                        Made Changes for GX Printing.
  22.                 10/12/95     CW                        Added call to InitDragHandlers in DoSVEdit
  23.                 10/12/95     CW                        Changed MaintainCursor to make cursor arrow if over selected text
  24.     
  25. */
  26.  
  27. #include <Memory.h>
  28. #include <Quickdraw.h>
  29. #include <Types.h>
  30. #include <Menus.h>
  31. #include <Windows.h>
  32. #include <Dialogs.h>
  33. #include <Traps.h>
  34. #include <Packages.h>
  35. #include <PPCToolBox.h>
  36. #include <Editions.h>
  37. #include <Printing.h>
  38. #include <ToolUtils.h>
  39. #include <Desk.h>
  40. #include <Scrap.h>
  41. #include <OSEvents.h>
  42. #include <AppleEvents.h>
  43. #include <AEObjects.h>
  44. #include <Errors.h>
  45. #include <Sound.h>
  46.  
  47. #include "SVEditMain.h"
  48.  
  49. #include "SVEditGlobals.h"
  50. #include "SVEditUtils.h"
  51. #include "SVAERecording.h"
  52. #include "SVAppleEvents.h"
  53. #include "SVEditWindow.h"
  54. #include "SVEditFile.h"
  55. #include "SVDrag.h"
  56.  
  57.  
  58. /*-----------------------------------------------------------------------*/
  59. /**----------                         Standard Main routines                  --------------**/
  60. /*-----------------------------------------------------------------------*/
  61.  
  62. #pragma segment Main
  63.  
  64.     pascal void MaintainCursor(void)
  65.         {
  66.             Point     pt;
  67.             WindowPtr wPtr;
  68.             GrafPtr   savePort;
  69.             DPtr      theDoc;
  70.  
  71.             wPtr = FrontWindow();
  72.             if (Ours(wPtr))
  73.                 {
  74.                     theDoc = DPtrFromWindowPtr(wPtr);
  75.                     GetPort(&savePort);
  76.                     SetPort(wPtr);
  77.                     GetMouse(&pt);
  78.                     if (theDoc->theText)
  79.                         if ( PtInRect ( pt, &(**(theDoc->theText)).viewRect ) &&
  80.                                                         !PointInWindowSelection ( pt, wPtr ) )
  81.                             SetCursor(&editCursor);
  82.                         else
  83.                             SetCursor(&qd.arrow);
  84.                     else
  85.                         SetCursor(&qd.arrow);
  86.  
  87.                     if (theDoc->theText)
  88.                         TEIdle(theDoc->theText);
  89.  
  90.                     SetPort(savePort);
  91.                 }
  92.         }
  93.  
  94. #pragma segment Main
  95.  
  96. pascal void MaintainMenus(void)
  97.     {
  98.         DPtr       theDoc;
  99.         WindowPtr  firstWindow;
  100.  
  101.         firstWindow = FrontWindow();
  102.         if (!Ours(firstWindow))
  103.             {
  104.                 EnableItem(myMenus[fileM], fmNew);
  105.                 EnableItem(myMenus[fileM], fmOpen);
  106.                 DisableItem(myMenus[fileM], fmClose);
  107.                 DisableItem(myMenus[fileM], fmSave);
  108.                 DisableItem(myMenus[fileM], fmSaveAs);
  109.                 DisableItem(myMenus[fileM], fmRevert);
  110.                 DisableItem(myMenus[fileM], fmPageSetUp);
  111.  
  112.                 DisableItem(myMenus[fileM], fmNoGXPrint);
  113.                                 
  114.                 if (firstWindow)
  115.                     {
  116.                         EnableItem(myMenus[editM], undoCommand);
  117.                         EnableItem(myMenus[editM], cutCommand);
  118.                         EnableItem(myMenus[editM], copyCommand);
  119.                         EnableItem(myMenus[editM], pasteCommand);
  120.                         EnableItem(myMenus[editM], clearCommand);
  121.                     }
  122.             }
  123.         else
  124.             {
  125.                 theDoc = DPtrFromWindowPtr(firstWindow);
  126.                 EnableItem(myMenus[editM], pasteCommand);
  127.                 EnableItem(myMenus[fileM], fmClose);
  128.                 EnableItem(myMenus[fileM], fmSaveAs);
  129.                 EnableItem(myMenus[fileM], fmPageSetUp);
  130.  
  131.                 EnableItem(myMenus[fileM], fmNoGXPrint);                
  132.  
  133.                 if (theDoc->dirty)
  134.                     EnableItem(myMenus[fileM], fmRevert);
  135.                 else
  136.                     DisableItem(myMenus[fileM], fmRevert);
  137.                     
  138.                 if ((theDoc->dirty) && (theDoc->everSaved))
  139.                     EnableItem(myMenus[fileM], fmSave);
  140.                 else
  141.                     DisableItem(myMenus[fileM], fmSave);
  142.  
  143.                 DisableItem(myMenus[editM], undoCommand);
  144.                 
  145.                 if (((**(theDoc->theText)).selEnd - (**(theDoc->theText)).selStart) < 0)
  146.                     {
  147.                         DisableItem(myMenus[editM], cutCommand);
  148.                         DisableItem(myMenus[editM], copyCommand);
  149.                         DisableItem(myMenus[editM], clearCommand);
  150.                     }
  151.                 else
  152.                     {
  153.                         EnableItem(myMenus[editM], cutCommand);
  154.                         EnableItem(myMenus[editM], copyCommand);
  155.                         EnableItem(myMenus[editM], clearCommand);
  156.                     }
  157.     }
  158.     
  159. }
  160.  
  161. #pragma segment Main
  162.  
  163. pascal void SetUpCursors(void)
  164.  
  165.     {
  166.         CursHandle  hCurs;
  167.  
  168.         hCurs = GetCursor(1);
  169.         editCursor = **hCurs;
  170.         hCurs = GetCursor(watchCursor);
  171.         waitCursor = **hCurs;
  172.     }
  173.  
  174. #pragma segment Main
  175.  
  176. pascal void SetUpMenus(void)
  177.     {
  178.         short i;
  179.         
  180.         myMenus[appleM] = GetMenu(appleID);
  181.         AppendResMenu(myMenus[appleM], 'DRVR');
  182.         myMenus[fileM] = GetMenu(fileID);
  183.         myMenus[editM] = GetMenu(editID);
  184.         myMenus[fontM] = GetMenu(mfontID);
  185.         AppendResMenu(myMenus[fontM], 'FONT');
  186.         myMenus[sizeM]  = GetMenu(sizeID);
  187.         myMenus[styleM] = GetMenu(styleID);
  188.  
  189.         for (i = appleM; i <= kLastMenu; i++)
  190.             InsertMenu(myMenus[i], 0);
  191.  
  192.         SetItemStyle(myMenus[styleM], cPlain, 0);
  193.         SetItemStyle(myMenus[styleM], cBold, bold);
  194.         SetItemStyle(myMenus[styleM], cItalic, italic);
  195.         SetItemStyle(myMenus[styleM], cUnderline, underline);
  196.         SetItemStyle(myMenus[styleM], cOutline, outline);
  197.         SetItemStyle(myMenus[styleM], cShadow, shadow);
  198.         SetItemStyle(myMenus[styleM], cCondense, condense);
  199.         SetItemStyle(myMenus[styleM], cExtend, extend);
  200.  
  201.         SetShortMenus(); /* Does a DrawMenuBar() */
  202.     }
  203.  
  204. pascal void DoFile(short theItem)
  205.     {        
  206.         short   alertResult;
  207.         DPtr    theDoc = nil;
  208.         FSSpec  theFSSpec;
  209.         OSErr   fileErr;
  210.         TPrint  thePSetup;
  211.         
  212.         switch (theItem)
  213.         {
  214.             case fmNew : IssueAENewWindow();
  215.                    break;
  216.             case fmOpen: if (GetFile(&theFSSpec)==noErr)
  217.                                          fileErr = IssueAEOpenDoc(theFSSpec);
  218.                    break;
  219.             case fmClose:IssueCloseCommand(FrontWindow());
  220.                    break;
  221.             case fmSave:
  222.             case fmSaveAs:
  223.                                         theDoc = DPtrFromWindowPtr(FrontWindow());
  224.                     
  225.                                         if (theDoc->everSaved == false || theItem == fmSaveAs)
  226.                                         {
  227.                                             fileErr = GetFileNameToSaveAs(theDoc);
  228.                                             if (fileErr == userCanceledErr)
  229.                                                 break;
  230.                                             else if (fileErr != noErr)
  231.                                                 FileError((unsigned char *)"\perror saving ", theDoc->theFileName);
  232.                                             else
  233.                                                 fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
  234.                 
  235.                                             if (fileErr == noErr)
  236.                                             {
  237.                                                 SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
  238.                                                 theDoc->everSaved = true;
  239.                                             }
  240.                                         }
  241.                                         else
  242.                                             fileErr = IssueSaveCommand(theDoc->theWindow, nil);
  243.                                         break;
  244.  
  245.             case fmRevert:
  246.                                         alertResult = DoFileDialog ( kRevertDialog, FrontWindow ( ) );
  247.                                         if ( alertResult == kStdOkItemIndex )
  248.                                         {
  249.                                             if (IssueRevertCommand(theDoc->theWindow))
  250.                                                 FileError((unsigned char *)"\perror reverting ", theDoc->theFileName);
  251.                                         }
  252.                                         break;
  253.  
  254.             case fmPageSetUp:
  255.                         theDoc = DPtrFromWindowPtr(FrontWindow());
  256.                                         if (DoPageSetup(theDoc))
  257.                                                 {
  258.                                                     thePSetup = **(theDoc->thePrintSetup);
  259.                                                      IssuePageSetupWindow(theDoc->theWindow, thePSetup);
  260.                                                  }
  261.                                         break;
  262.  
  263.             case fmPrint: IssuePrintWindow(FrontWindow(),kUsePrintDialog);
  264.                                       break;
  265.             case fmPrintOne: IssuePrintWindow(FrontWindow(),kNoPrintDialog);
  266.                                       break;
  267.  
  268.             case fmQuit : IssueQuitCommand();
  269.                     break;
  270.         }                 /*of switch*/
  271.     }
  272.  
  273. #pragma segment Main
  274.  
  275. pascal void DoCommand(long mResult)
  276.     {
  277.         short   theItem;
  278.         short   err;
  279.         long    result;
  280.         Str255  name;
  281.         DPtr    theDocument;
  282.  
  283.         theDocument = DPtrFromWindowPtr(FrontWindow());
  284.         
  285.         theItem = LoWord(mResult);
  286.  
  287.         switch (HiWord(mResult)){
  288.  
  289.             case appleID:
  290.                 if (theItem == aboutItem)
  291.                     {
  292.                         SetCursor(&qd.arrow);
  293.                         result = Alert(258, nil);
  294.                     }
  295.                 else
  296.                     {
  297.                         GetMenuItemText(myMenus[appleM], theItem, name);
  298.                         err = OpenDeskAcc(name);
  299.                         SetPort(FrontWindow());
  300.                     }
  301.         break;
  302.                 
  303.             case fileID: DoFile(theItem);
  304.                          break;
  305.  
  306.             case editID:
  307.                     if (SystemEdit(theItem - 1) == false)
  308.                     {
  309.                         switch (theItem){
  310.     
  311.                             case cutCommand   : IssueCutCommand(theDocument);
  312.                                break;
  313.                                                          
  314.                             case copyCommand  : IssueCopyCommand(theDocument);
  315.                                break;
  316.                                                          
  317.                             case pasteCommand : IssuePasteCommand(theDocument);
  318.                                break;
  319.                                                          
  320.                             case clearCommand : IssueClearCommand(theDocument);
  321.                                break;
  322.                                                          
  323.                             case selectAllCommand :
  324.                                                          if (theDocument)
  325.                                                              TESetSelect(0,
  326.                                                                                      (**(theDocument->theText)).teLength,
  327.                                                                                      theDocument->theText);
  328.                                break;
  329.                         }     /*of switch*/
  330.                         ShowSelect(theDocument);
  331.                     }
  332.           break;
  333.  
  334.             case mfontID: IssueFontCommand(theDocument, theItem);
  335.                             break;
  336.  
  337.             case sizeID: IssueSizeCommand(theDocument, theItem);
  338.                             break;
  339.  
  340.             case styleID: IssueStyleCommand(theDocument, theItem);
  341.                              break;
  342.  
  343.         }                 /*of switch*/
  344.  
  345.         HiliteMenu(0);
  346.  
  347.     }
  348.  
  349. #pragma segment Main
  350.  
  351.  
  352.     pascal void DoMouseDown(const EventRecord *myEvent)
  353.         {
  354.             WindowPtr whichWindow;
  355.             Point     p;
  356.             Rect      dragRect;
  357.             DPtr      theDoc;
  358.  
  359.             p = myEvent->where;
  360.             switch (FindWindow(p, &whichWindow)){
  361.  
  362.                 case inDesk: SysBeep(10);
  363.                              break;
  364.  
  365.                 case inGoAway:if (Ours(whichWindow))
  366.                                                 if (TrackGoAway(whichWindow, p))
  367.                                                     IssueCloseCommand(whichWindow);
  368.                                             break;
  369.                 case inMenuBar:
  370.                                             SetCursor(&qd.arrow);
  371.                                             theDoc = DPtrFromWindowPtr(FrontWindow());
  372.                                             if (theDoc)
  373.                                                 {
  374.                                                     SetFontMenu(theDoc);
  375.                                                 }
  376.                     
  377.                                             DoCommand(MenuSelect(p));
  378.                                             HiliteMenu(0);
  379.                                             break;
  380.  
  381.                 case inSysWindow: SystemClick(myEvent, whichWindow);
  382.                                   break;
  383.  
  384.                 case inDrag:
  385.                     
  386.                         dragRect = qd.screenBits.bounds;
  387.                         
  388.                         if (Ours(whichWindow))
  389.                             {
  390.                                 DragWindow(whichWindow, p, &dragRect);
  391.                                 /*
  392.                                     As rgnBBox may be passed by address
  393.                                 */
  394.                                 dragRect = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
  395.                                 /*
  396.                                     The windows already there, but still tell
  397.                                     the our AppleEvents core about the move in case
  398.                                     they want to do anything
  399.                                 */
  400.                                 IssueMoveWindow(whichWindow, dragRect);
  401.                             }
  402.                       break;
  403.  
  404.                 case inGrow:SetCursor(&qd.arrow);
  405.                                         if (Ours(whichWindow))
  406.                                             MyGrowWindow(whichWindow, p);
  407.                                         break;
  408.                                     
  409.                 case inZoomIn : DoZoom(whichWindow, inZoomIn, p);
  410.                                               break;
  411.  
  412.                 case inZoomOut: DoZoom(whichWindow, inZoomOut, p);
  413.                         break;
  414.                 case inContent: if (whichWindow != FrontWindow())
  415.                                                     SelectWindow(whichWindow);
  416.                                                 else
  417.                                                     if (Ours(whichWindow))
  418.                                                         DoContent(whichWindow, *myEvent);
  419.                         break;
  420.             }                 /*of switch*/
  421.         }
  422.  
  423. #pragma segment Main
  424.  
  425. pascal long GetSleep(void)
  426.     {
  427.         long      sleep;
  428.         WindowPtr theWindow;
  429.         DPtr      theDoc;
  430.  
  431.         sleep = 0x7fffffff;
  432.         if (!gInBackground)
  433.             {
  434.                 theWindow = FrontWindow();
  435.                 if (theWindow)
  436.                     if (Ours(theWindow))
  437.                         {
  438.                             theDoc = DPtrFromWindowPtr(theWindow);
  439.                             if ((**(theDoc->theText)).selStart == (**(theDoc->theText)).selEnd)
  440.                                 sleep = GetCaretTime();
  441.                         }
  442.             }
  443.         return(sleep);
  444.     }                     /*GetSleep*/
  445.  
  446. #pragma segment Main
  447.  
  448. void HandleOneEvent( EventRecord *myEvent )
  449.     {
  450.         DPtr        theDoc;
  451.         char        theChar;
  452.         WindowPtr   theWindow;
  453.         Boolean     activate;
  454.         
  455.         switch (myEvent->what) {
  456.             case mouseDown: FlushAndRecordTypingBuffer();
  457.                                             DoMouseDown(myEvent);
  458.                             break;
  459.             case keyDown:
  460.             case autoKey:
  461.                         theDoc = DPtrFromWindowPtr(FrontWindow());
  462.  
  463.                         theChar = myEvent->message & charCodeMask;
  464.  
  465.                         if ((myEvent->modifiers & cmdKey) == cmdKey)
  466.                             {
  467.                                 DoCommand(MenuKey(theChar));
  468.                                 HiliteMenu(0);
  469.                             }
  470.                         else
  471.                             if (theDoc->theText)
  472.                                 {
  473.                                     AddKeyToTypingBuffer(theDoc, theChar);
  474.                                     
  475.                                     TEKey(theChar, theDoc->theText);
  476.                                     AdjustScrollbars(theDoc, false);
  477.                     
  478.                                     ShowSelect(theDoc);
  479.                                     
  480.                                     theDoc->dirty = true;
  481.                                         }
  482.                       break;
  483.                 
  484.             case activateEvt:
  485.                     activate = ((myEvent->modifiers & activeFlag) != 0);
  486.                     theWindow = (WindowPtr)myEvent->message;
  487.                     DoActivate(theWindow, activate);
  488.                   break;
  489.  
  490.             case updateEvt:
  491.                     DoUpdate ( (WindowPtr) myEvent->message );
  492.                   break;
  493.  
  494.             case kHighLevelEvent: FlushAndRecordTypingBuffer();
  495.                                                         DoAppleEvent(*myEvent);
  496.                             break;
  497.             case kOSEvent:
  498.                 
  499.                 switch (myEvent->message & osEvtMessageMask) { /*high byte of message*/
  500.                     case 0x01000000:
  501.                         {
  502.                             gInBackground = ((myEvent->message & resumeFlag) == 0);
  503.                             DoActivate(FrontWindow(), !gInBackground);
  504.                         }
  505.                 }
  506.             } /*of switch*/
  507.     }
  508.     
  509. #pragma segment Main
  510.  
  511. pascal void MainEvent(void)
  512.     {
  513.         EventRecord myEvent;
  514.  
  515.         MaintainCursor(); /* TEIdle in here for now */
  516.         MaintainMenus();
  517.  
  518.         if (WaitNextEvent(everyEvent, &myEvent, GetSleep(), nil))
  519.             HandleOneEvent( &myEvent );
  520.         }
  521.  
  522. #pragma segment Main
  523.  
  524. pascal void DoSVEdit(void)
  525.     {
  526.         OSErr  err;
  527.         short  result;
  528.  
  529.         InitGraf(&qd.thePort);
  530.         InitFonts();
  531.         FlushEvents(everyEvent, 0);
  532.         InitWindows();
  533.         InitMenus();
  534.         TEInit();
  535.         InitDialogs(nil);
  536.         InitCursor();
  537.  
  538.         MaxApplZone();
  539.         SetUpCursors();
  540.  
  541.         SetUpMenus();
  542.  
  543.         gWCount    = 0;
  544.         gNewDocCount = 0;
  545.         gQuitting  = false;
  546.         gFontMItem = 0;
  547.  
  548.         gGestaltAvailable          = false;
  549.         gAppleEventsImplemented    = false;
  550.         gAliasManagerImplemented   = false;
  551.         gEditionManagerImplemented = false;
  552.         gOutlineFontsImplemented   = false;
  553.  
  554.         /*check environment checks to see if we are running 7.0*/
  555.         if (!CheckEnvironment())
  556.             {
  557.                 SetCursor(&qd.arrow);
  558.                 /*pose the only 7.0 alert*/
  559.                 result = Alert(302, nil);
  560.                 return;
  561.             }
  562.  
  563.         err = InitEditionPack();
  564.  
  565.         if (err)
  566.             {
  567.                 ShowError((unsigned char *)"\pInitEditionPack", err);
  568.                 gQuitting = true;
  569.             }
  570.  
  571.         err = AEObjectInit();
  572.         if (err)
  573.             {
  574.                 ShowError((unsigned char *)"\pAEObjectInit", err);
  575.                 gQuitting = true;
  576.             }
  577.  
  578.         InitAppleEvents();
  579.  
  580.         err = PPCInit();
  581.         if (err)
  582.             {
  583.                 ShowError((unsigned char *)"\pPPCInit", err);
  584.                 gQuitting = true;
  585.             }
  586.             
  587.             
  588.         err = InitDragHandlers ( );
  589.         if (err)
  590.             {
  591.                 ShowError((unsigned char *)"\pInitDragHandlers", err);
  592.                 gQuitting = true;
  593.             }
  594.         
  595.             
  596.         /* create the UPPs for the control tracking routines */
  597.         /* globals, since we don't want to have to create and dispose of the routine */
  598.         /* descriptors each time we call DoContent */
  599.         gHScrollActionUPP = NewControlActionProc ( HActionProc );
  600.         gVScrollActionUPP = NewControlActionProc ( VActionProc );
  601.         
  602.         /* create a UPP for drawing a dialog's default button. Again, make it a global */
  603.         /* for convenience */
  604.         gDefaultButtonUPP = NewUserItemProc(DrawDefaultOutline);
  605.  
  606.  
  607.         while (!gQuitting)
  608.             MainEvent();
  609.     }
  610.     
  611. void main ()
  612.     {
  613.         /*the main routine starts here*/
  614.         DoSVEdit();
  615.     }